package com.qq.BFMRSE.Module;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import javax.servlet.http.HttpServletResponse;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import com.qq.BFMRSE.Hdfs.hdfsOper;
import com.qq.BFMRSE.util.BFConstants;

public class downloadTool {
	private String userName;
	private int downType;
	private String tmpFile;
	public String getTmpFile() {
		return tmpFile;
	}
	public void setTmpFile(String tmpFile) {
		this.tmpFile = tmpFile;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public int getDownType() {
		return downType;
	}
	public void setDownType(int downType) {
		this.downType = downType;
	}
	public downloadTool(String userName,int downType,String tmpFile){
		this.userName=userName;
		this.tmpFile=tmpFile;
		if(downType==1){
			this.downType=BFConstants.DOWNLOADSINGLE;
		}else{
			this.downType=BFConstants.DOWNMULTIFILES;
		}
	}
	public downloadTool(){}
	public void downloadFile(String[] object,HttpServletResponse response)
	{
		if(downType==1){
			downLoadSingleFile(object, response);
		}else if(downType==2){
			downLoadMultiFiles(object, response);
		}
	}
	/**
	 * put the single into httpServletResponse
	 * @param src
	 * @param response
	 * @throws IOException
	 */
	public void downLoadSingleFile(String []fileNames,HttpServletResponse response)
	{
		if(fileNames.length==1){
			String srcPath=BFConstants.HADOOP_PATH+File.separator+userName+File.separator+BFConstants.ENCRYDIRNAME+File.separator+fileNames[0];;
			Configuration conf=new Configuration();
			FileSystem hdfs=null;
			try {
				hdfs = FileSystem.get(URI.create(srcPath),conf);
				Path path=new Path(srcPath);
				InputStream in=hdfs.open(path);
			    OutputStream os=response.getOutputStream();
				BufferedInputStream bin=new BufferedInputStream(in);
			    int length=0;
			    byte[]buffer=new byte[2048];
			    while((length=bin.read(buffer))>0)
			    {
			    	os.write(buffer, 0, length);
			    }
			    os.close();
			    bin.close();
			    in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				 
					try {
						hdfs.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}

	}
	public void downLoadMultiFiles(String [] fileNames,HttpServletResponse response){
		String tmpZipName=tmpFile+File.separator+System.currentTimeMillis()+BFConstants.TMPZIPNAME;
		hdfsOper.addAllFilesToZip(fileNames, userName, tmpZipName);
		OutputStream os=null;
		BufferedInputStream bin=null;
		try {
			os = response.getOutputStream();
			bin=new BufferedInputStream(new FileInputStream(tmpZipName));
		    int length=0;
		    byte[]buffer=new byte[2048];
		    while((length=bin.read(buffer))>0)
		    {
		    	os.write(buffer, 0, length);
		    }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		    try {
				os.close();
			    bin.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}
